<template>
	<!-- 陆亿版本 -->
	<view>
		<!-- 头部搜索框 -->
		<search :placeholder="$t('public.cutNo')" />
		<view v-show="this.dataList.length>0">
			<zy-table :headList="headList" :dataList="dataList" @editConfirm="editConfirm" border></zy-table>
			<bottomBtn fixed :leftText="$t('public.reset')" :rightText="$t('public.submit')" @rightBtn="submitBtn"
				@leftBtn="resetBtn"></bottomBtn>
		</view>
		<zy-empty v-if="this.dataList.length==0"></zy-empty>
		<!-- 提交确认 -->
		<zy-popup ref="popup" :title="$t('homepage.WarehouseManage.outstorageSure')" type="dialog" @confirm="submit">
			{{$t('homepage.AccessoryManagement.isSureOutstorage')}}
		</zy-popup>
	</view>
</template>
<script>
	import {
		cuttingSheetCardLists,
		outCscWarehouse
	} from "@/api/homepage.js"
	import audio from '@/common/audio.js' // 语音播报
	export default {
		data() {
			return {
				//表格表头
				headList: [{
					name: this.$t('public.cutNo'),
					key: 'cutNo',
				}, {
					name: this.$t('public.num'),
					key: 'number',
					type: 'number',
					sum: true
				}, {
					name: this.$t('public.deliveryNum'),
					key: 'deliveryNum',
					edit: true,
					type: 'number',
					sum: true
				}],
				dataList: [], //表格数据
			}
		},
		onLoad() {
			//PDA扫描
			var _this = this
			uni.$on('scancodedate', function(data) {
				//清除字符串空格 
				_this.searchList(data.code)
			})
		},
		onUnload() {
			// 移除监听事件      
			uni.$off('scancodedate')
		},
		methods: {
			searchList(e) {
				cuttingSheetCardLists({
					cuttingCuttingNo: e,
				}).then((res) => {
					if (res.code == 0) {
						console.log(res.data)
						if (this.dataList.findIndex(m => m.cutNo === e) !== -1) {
							uni.showToast({
								title: this.$t('public.searchRepeat'),
								icon: "none",
								duration: 3000
							})
							audio.getInnerAudio('../../../../../static/repeat.wav')
						} else {
							res.data.forEach((item, index) => {
								this.dataList.push({
									barcode: item.barcode,
									cutNo: item.cuttingNo,
									number: item.number,
									deliveryNum: 0,
								})
							})
							audio.getInnerAudio('../../../../static/scanSuccess.wav')
						}
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
						audio.getInnerAudio('../../../../../static/scanFail.wav')
					}
				}).catch((err) => {
					uni.showToast({
						title: err.message,
						icon: "none",
						duration: 3000
					})
					audio.getInnerAudio('../../../../../static/scanFail.wav')
				})
			},
			//编辑出库数量
			editConfirm(val) {
				if (Number(val.item.deliveryNum) > Number(this.dataList[val.index].num)) {
					this.dataList[val.index].deliveryNum = this.dataList[val.index].num
					uni.showToast({
						title: this.$t('public.max') + this.dataList[val.index]
							.surplusNum,
						icon: "none",
						duration: 3000
					})
				}
			},
			// 页面重置
			resetBtn() {
				uni.redirectTo({
					url: "/pages/HomePage/CutMarket/BatchOutStorage/index"
				})
			},
			//入库提交
			submitBtn() {
				this.$refs.popup.open()
			},
			//弹框确认
			submit() {
				let barcode = ""
				let deliveryNum = ""
				for (var i = 0; i < this.dataList.length; i++) {
					barcode += this.dataList[i].barcode + ','
					deliveryNum += this.dataList[i].deliveryNum + ','
				}
				outCscWarehouse({
					barcode: barcode,
					num: deliveryNum
				}).then((res) => {
					if (res.code == 0) {
						this.resetBtn()
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
		}
	}
</script>
<style lang="scss" scoped>
</style>